//
//  HTClassViewController.swift
//  QuitSmokingApp
//
//  Created by   on 2024/3/6.
//

import UIKit
import Combine
import SnapKit

class HTClassViewController: UIViewController {
    var var_set = Set<AnyCancellable>()
    lazy var var_bg_imageView: UIImageView = {
        let var_view = UIImageView()
        var_view.image = UIImage(named: "icon_theme_01")
        return var_view
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        ht_changeThem()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.ht_setUI()
        self.view.backgroundColor = HTClassTool.STATIC_background
        ht_navigationConfiguration()
        ht_subscribeNotification()
    }
    func ht_setUI() {
        self.view.addSubview(self.var_bg_imageView)
        var_bg_imageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    func ht_subscribeNotification() {
        // 创建通知发布者
        let var_notificationPublisher = NotificationCenter.default.publisher(for: Notification.Name(var_ChangeNavigationBarcColor))
        // 创建订阅者
        var_notificationPublisher
          .subscribe(on: DispatchQueue.main)
          .sink(receiveValue: { [weak self] notification in
              self?.ht_navigationConfiguration()
          })
          .store(in: &var_set)
    }
    func ht_changeNavBarColor() {
        NotificationCenter.default.post(name: Notification.Name(var_ChangeNavigationBarcColor), object: nil, userInfo: nil)
    }
    func ht_navigationConfiguration() {
        let var_appearance = UINavigationBarAppearance()
        var_appearance.configureWithOpaqueBackground()
//        var_appearance.backgroundColor = HTClassTool.ht_getNavigationBack() // 设置背景颜色
        var_appearance.backgroundColor = .clear
        
        let var_navigationBarAppearance = UINavigationBarAppearance()
        var_navigationBarAppearance.configureWithOpaqueBackground()
        var_navigationBarAppearance.backgroundColor = .clear
        var_navigationBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white, .font: UIFont.systemFont(ofSize: 20, weight: .bold) ]
        var_navigationBarAppearance.shadowColor = .clear
        var_navigationBarAppearance.shadowImage = UIImage()
        self.navigationController?.navigationBar.standardAppearance = var_appearance
        self.navigationController?.navigationBar.compactAppearance = var_navigationBarAppearance
        self.navigationController?.navigationBar.scrollEdgeAppearance = var_navigationBarAppearance
    }
    
    func ht_changeThem() {
        if HTClassStorage.ht_getNavBg() == "1" {
            var_bg_imageView.image = UIImage(named: "icon_theme_02")
        } else if HTClassStorage.ht_getNavBg() == "2" {
            var_bg_imageView.image = UIImage(named: "icon_theme_03")
        } else if HTClassStorage.ht_getNavBg() == "3" {
            var_bg_imageView.image = UIImage(named: "icon_theme_04")
        } else {
            var_bg_imageView.image = UIImage(named: "icon_theme_01")
        }
    }
}
